package com.beiing.tianshuai.tianshuai.interest.model;

import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.interest.entity.InterestBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Jinni on 2017/11/19 0019
 */

public class InterestModel implements InterestModelImpl{

    private static final String TAG = "InterestModel";
    private OnInterestResponseListener mListener;

    public InterestModel(OnInterestResponseListener listener) {
        mListener = listener;
    }

    @Override
    public void getInterestInfo(String typeName, String pageSize, String lastId, String uid, int pageNum) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("type_name", typeName)
                .addFormDataPart("pageSize", pageSize)
                .addFormDataPart("lastId", lastId)
                .addFormDataPart("uid", uid)
                .addFormDataPart("page", String.valueOf(pageNum))
                .build();
        Observable<InterestBean> observable = DataManager.getInterestInfo(body);
        Observable<InterestBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<InterestBean>() {
            @Override
            public void onNext(InterestBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onError(e, 0);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void getPriseResult(String vid, String uid, String controller) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("vid", vid)
                .addFormDataPart("uid", uid)
                .addFormDataPart("controller", controller)
                .build();
        Observable<CodeBean> observable = DataManager.getPriseResult(body);
        Observable<CodeBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(CodeBean bean) {

            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onError(e, 0);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("顶踩请求结束", TAG);
            }
        });
    }

    @Override
    public void getVideoReport(String vid, String uid, String content) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("vid", vid)
                .addFormDataPart("uid", uid)
                .addFormDataPart("content", content)
                .build();
        Observable<CodeBean> observable = DataManager.getReportResult(body);
        Observable<CodeBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(CodeBean bean) {
                if (mListener != null) {
                    mListener.onReportSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onError(e, 0);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("举报请求结束", TAG);
            }
        });
    }

    public interface OnInterestResponseListener {
        void onSuccess(InterestBean bean);
        void onReportSuccess(CodeBean bean);
        void onError(Throwable throwable, int errorType);
    }
}
